package com.feimt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feimt.pojo.ShoppingCart;
import com.feimt.result.R;
import com.feimt.service.ShoppingCartService;
import com.feimt.utils.BaseContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 宋孟林
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
@Transactional
public class ShoppingCartController {
    private ShoppingCartService shoppingCartService;

    @Autowired
    public ShoppingCartController(ShoppingCartService shoppingCartService) {
        this.shoppingCartService = shoppingCartService;
    }

    @PostMapping("/add")
    public R<ShoppingCart> add1(@RequestBody ShoppingCart shoppingCart){
        log.info("需要添加到购物车的菜为：{}", shoppingCart);
        Long id = BaseContent.getThreadLocal();
        shoppingCart.setUserId(id);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        if (dishId != null){
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if (cart != null){
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            shoppingCartService.updateById(cart);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId).eq(ShoppingCart::getUserId, BaseContent.getThreadLocal());
            ShoppingCart cart = shoppingCartService.getOne(wrapper);
            cart.setNumber(cart.getNumber() - 1);
            Integer number = cart.getNumber();
            if (number > 0) {
                shoppingCartService.updateById(cart);
            }else if (number == 0){
                shoppingCartService.removeById(cart.getId());
            }else {
                return R.error("操作异常");
            }
            return R.success(cart);
        }
        Long setmealId = shoppingCart.getSetmealId();
        if (setmealId != null){
            wrapper.eq(ShoppingCart::getSetmealId, setmealId).eq(ShoppingCart::getUserId, BaseContent.getThreadLocal());
            ShoppingCart cart1 = shoppingCartService.getOne(wrapper);
            cart1.setNumber(cart1.getNumber() - 1);
            Integer number = cart1.getNumber();
            if (number > 0){
                shoppingCartService.updateById(cart1);
            }else if (number == 0){
                shoppingCartService.removeById(cart1.getId());
            }else {
                return R.error("操作异常");
            }
            return R.success(cart1);
        }
        return R.error("操作异常");
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContent.getThreadLocal());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> delete(){
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContent.getThreadLocal());
        shoppingCartService.remove(wrapper);
        return R.success("清楚购物车成功");
    }
}
